package com.untilov.gb.me;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.untilov.gb.FeedsBuilder;
import com.untilov.gb.me.gui.CategoriesList;
import com.untilov.gb.me.gui.CategoryContentList;
import com.untilov.gb.me.gui.FeedDetailsForm;
import com.untilov.gb.me.gui.LoginForm;
import com.untilov.gb.model.FeedItem;

/**
 * Application GUI for Java ME enabled device.
 * 
 * @author iuntilov
 * 
 */
public class GoogleBookmarksMIDlet extends MIDlet {

    /**
     * Login form.
     */
    private LoginForm loginForm;

    private Alert loginFailed;

    /**
     * List of categories.
     */
    private CategoriesList categoriesList;
    
    /**
     * List of bookmarks in the specified category.
     */
    private CategoryContentList categoryContentList;
    
    /**
     * Form that contains details of selected feed(bookmark)
     */
    private FeedDetailsForm feedDetailsForm;

    /**
     * Contains all labels.
     */
//    private List labelList;
    
    /**
     * Data retriever
     */
    private FeedsBuilder feedsBuilder;

    /**
     * The HelloMIDlet constructor.
     */
    public GoogleBookmarksMIDlet() {
        loginForm = new LoginForm("Input Google credentials", this);
        // Alert if login failed.
        loginFailed = new Alert(
                "Login failed",
                "Problems during login, check login, password, connection and try again.",
                null, AlertType.ERROR);
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void pauseApp() {

    }

    public void startApp() throws MIDletStateChangeException {
        getDisplay().setCurrent(loginForm);

    }

    /**
     * Get display of the application.
     * 
     * @return
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    public Alert getLoginFailed(String text) {
        if (text != null) {
            loginFailed.setString(loginFailed.getString() + text);
        }
        return loginFailed;
    }

    /**
     * Shows login form.
     */
    public void showLoginForm() {
        getDisplay().setCurrent(loginForm);
    }

    /**
     * Shows list of bookmars categories.
     */
    public void showCategoriesList() {
        getDisplay().setCurrent(getCategoriesList());
    }
    
    /**
     * Show content for specified category.
     * @param category
     */
    public void showCategoryContent(String category) {
        getDisplay().setCurrent(getCategoryContentList().fillListForCategory(category));
    }

    /**
     * Get list of categories. 
     * @return
     */
    public CategoriesList getCategoriesList() {
        if (categoriesList == null) {
            categoriesList = new CategoriesList("Select category",
                    List.IMPLICIT, this);
        }
        return categoriesList;
    }

    public FeedsBuilder getFeedsBuilder() {
        return feedsBuilder;
    }

    public void setFeedsBuilder(FeedsBuilder feedsBuilder) {
        this.feedsBuilder = feedsBuilder;
    }

    public CategoryContentList getCategoryContentList() {
        if (categoryContentList == null) {
            categoryContentList = new CategoryContentList("Select bookmark", 
                    List.IMPLICIT, this);
        }
        return categoryContentList;
    }
    
    public FeedDetailsForm getFeedDetailsForm() {
        if (feedDetailsForm == null) {
            feedDetailsForm = new FeedDetailsForm("Bookmarks details", this);
        }
        return feedDetailsForm;
    }
    
    public void showFeedDetailsForm(FeedItem item) {
        this.getDisplay().setCurrent(getFeedDetailsForm().showBookmark(item));
    }
}
